package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.help.fuwenben.MClickableSpan;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，隐私
 */
public class PrivacyDialog extends Dialog {

    private TextView titleTv;//消息标题文本
    private TextView mText;
    private TextView mNo;
    private TextView mYes;
    private TextView mTextDetail;

    private String titleStr;//从外界设置的title文本
    private String messageStr, detailsStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
    private onRegistOnclickListener registOnclickListener;
    private onPrivacyOnclickListener privacyOnclickListener;

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    public void setRegistOnclickListener(onRegistOnclickListener listener) {
        this.registOnclickListener = listener;
    }

    public void setPrivacyOnclickListener(onPrivacyOnclickListener listener) {
        this.privacyOnclickListener = listener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public PrivacyDialog(Context context) {
        super(context, R.style.DialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.privacy_dialog_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //按了返回键时 也不消失dismiss
        setCancelable(false);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        mYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                    dismiss();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        mNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                    dismiss();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            mText.setText(messageStr);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            mYes.setText(yesStr);
        }
        if (noStr != null) {
            mNo.setText(noStr);
        }
        SpannableStringBuilder textSpannedBuilder = new SpannableStringBuilder();
        SpannableString textSpanned1 = new SpannableString(getContext().getString(R.string.string_privacy_details));
        textSpanned1.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString textSpanned2 = new SpannableString(getContext().getString(R.string.string_agreenment_user));
        textSpanned2.setSpan(new ForegroundColorSpan(Color.parseColor("#ED4E4E")), 0, textSpanned2.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        MClickableSpan clickableSpan = new MClickableSpan() {
            @Override
            public void onClick(View view) {
                if (registOnclickListener != null) {
                    registOnclickListener.onClick();
                }
            }
        };
        textSpanned2.setSpan(clickableSpan,
                0, textSpanned2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableString textSpanned3 = new SpannableString(getContext().getString(R.string.string_as_well_as));
        textSpanned3.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, textSpanned3.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString textSpanned4 = new SpannableString(getContext().getString(R.string.string_agreenment_privacy));
        textSpanned4.setSpan(new ForegroundColorSpan(Color.parseColor("#ED4E4E")), 0, textSpanned4.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        MClickableSpan clickableSpan4 = new MClickableSpan() {
            @Override
            public void onClick(View view) {
                if (privacyOnclickListener != null) {
                    privacyOnclickListener.onClick();
                }
            }
        };
        textSpanned4.setSpan(clickableSpan4,
                0, textSpanned4.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableString textSpanned5 = new SpannableString(getContext().getString(R.string.string_full_stop));
        textSpanned5.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, textSpanned5.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        //注意：此时必须加这一句，不然点击事件不会生效
        mTextDetail.setMovementMethod(LinkMovementMethod.getInstance());
        mTextDetail.setText(textSpannedBuilder.append(textSpanned1).append(textSpanned2).append(textSpanned3).append(textSpanned4).append(textSpanned5));
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        titleTv = (TextView) findViewById(R.id.title);
        mText = (TextView) findViewById(R.id.text);
        mNo = (TextView) findViewById(R.id.no);
        mYes = (TextView) findViewById(R.id.yes);
        mTextDetail = (TextView) findViewById(R.id.text_detail);

    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param url
     */
    public void setMessage(String url) {
        messageStr = url;
    }


    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }

    public interface onRegistOnclickListener {
        public void onClick();
    }

    public interface onPrivacyOnclickListener {
        public void onClick();
    }
}
